﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class administration_listOffers : System.Web.UI.Page
{
    private string IDEN, UPDATEPARAM, NEWPARAM, MESSAGEPARAM, DELETEPARAM, PATHIMAGE = System.Configuration.ConfigurationSettings.AppSettings["pathImage"];
    private const string IDPAGE = "7";

    protected void Page_Load(object sender, EventArgs e)
    {
        //Check the status of the session
        STATE_SESSION();

        //Verify the view that corresponds show
        if (!Page.IsPostBack)
        {
            string flgInsert = Request.Cookies.Get("flgInsert").Value;
            string flgUpdate = Request.Cookies.Get("flgUpdate").Value;
            string flgDelete = Request.Cookies.Get("flgDelete").Value;

            this.IDEN = Request.Params.Get("id");
            this.NEWPARAM = Request.Params.Get("new");
            this.UPDATEPARAM = Request.Params.Get("update");
            this.MESSAGEPARAM = Request.Params.Get("message");
            this.DELETEPARAM = Request.Params.Get("delete");

            if (this.NEWPARAM != null)
            {
                MOffer.ActiveViewIndex = 1;
            }
            else
            {
                if (this.UPDATEPARAM != null)
                {
                    MOffer.ActiveViewIndex = 2;
                }
                else
                {
                    if (this.MESSAGEPARAM != null)
                    {
                        MOffer.ActiveViewIndex = 3;
                        String nameupdate = Request.Params.Get("nameupdate");
                        String namenew = Request.Params.Get("namenew");
                        String removal = Request.Params.Get("removal");
                        if (namenew != null)
                        {
                            this.lblMessage.Text = "It successfully register at: " + namenew;
                        }
                        else
                        {
                            if (nameupdate != null)
                            {
                                this.lblMessage.Text = "The update was successful.";
                            }
                            else
                            {
                                if (removal != null)
                                {
                                    if (removal.Equals("yes"))
                                    {
                                        this.lblMessage.Text = "Successful removal.";
                                    }
                                    else
                                    {
                                        this.lblMessage.Text = "registration is being used, therefore can not be eliminated";
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (this.DELETEPARAM != null)
                        {
                            this.DeleteRecord(this.IDEN);                            
                        }
                        MOffer.ActiveViewIndex = 0;
                    }
                }
            }
            intPageSize.Text = "10"; //Here indicates the size of the page
            intCurrIndex.Text = "0";
            Bind(this.txtSearch.Text);
            //Disable the checks after loading the datalist
            UpdateInsertDelete(flgUpdate, flgInsert, flgDelete);
            //Built the menu according to user roles
            BuildMenu();
        }
    }

    //Method to verify the status of the current session
    public void STATE_SESSION()
    {
        if (!Page.IsPostBack)
        {
            HttpCookie state = Request.Cookies.Get("state");
            if (state == null)
                Response.Redirect("../index.aspx");
        }
    }

    //Method to verify the user's role
    private void UpdateInsertDelete(string update, string insert, string delete)
    {
        if (!insert.Equals("1"))
        {
            lnkNew.Enabled = false;
        }
        if (!update.Equals("2"))
        {
            foreach (DataListItem item in DataList1.Items)
            {
                HyperLink hp = null;
                hp = (HyperLink)item.FindControl("lnkUpdate");
                if (hp != null)
                {
                    hp.Enabled = false;
                }
            }
        }
        if (!delete.Equals("3"))
        {
            foreach (DataListItem item in DataList1.Items)
            {
                HyperLink hp = null;
                hp = (HyperLink)item.FindControl("lnkDelete");
                if (hp != null)
                {
                    hp.Enabled = false;
                }
            }
        }
    }

    //Method to build menu
    private void BuildMenu()
    {
        string programName = Request.Cookies.Get("programName").Value;
        char[] delimiterChars = { ',' };
        string[] vectorProgramName = programName.Split(delimiterChars);
        string welcome = "";
        string segurity = "";
        string system = "";
        string parameters = "";
        string people = "";
        string operations = "";
        string reports = "";
        for (int i = 0; i < vectorProgramName.Length; i++)
        {
            switch (Convert.ToInt32(vectorProgramName[i]))
            {
                case 1:
                    people += "<li><a class=\"item\" href=\"listNaturalPersons.aspx\"> - Natural Persons</a></li>";
                    break;
                case 2:
                    people += "<li><a class=\"item\" href=\"listLegalPersons.aspx\"> - Legal Persons</a></li>";
                    break;
                case 3:
                    welcome += "<li><a class=\"item\" href=\"index.aspx\"> - Welcome</a></li>";
                    break;
                case 4:
                    parameters += "<li><a class=\"item\" href=\"listCategory.aspx\"> - Category</a></li>";
                    break;
                case 5:
                    system += "<li><a class=\"item\" href=\"listLogTransaction.aspx\"> - Log Transactions</a></li>";
                    break;
                case 6:
                    operations += "<li><a class=\"item\" href=\"listNews.aspx\"> - News</a></li>";
                    break;
                case 7:
                    operations += "<li><a class=\"item\" href=\"listOffers.aspx\"> - Offers</a></li>";
                    break;
                case 8:
                    segurity += "<li><a class=\"item\" href=\"listProfiles.aspx\"> - Profiles</a></li>";
                    break;
                case 9:
                    system += "<li><a class=\"item\" href=\"listPrograms.aspx\"> - Programs</a></li>";
                    break;
                case 10:
                    operations += "<li><a class=\"item\" href=\"listProyects.aspx\"> - Proyects</a></li>";
                    break;
                case 11:
                    segurity += "<li><a class=\"item\" href=\"listRoles.aspx\"> - Roles</a></li>";
                    break;
                case 12:
                    segurity += "<li><a class=\"item\" href=\"listUsers.aspx\"> - Users</a></li>";
                    break;
                case 13:
                    reports += "<li><a class=\"item\"href=\"#\"> - Proyects</a></li>";
                    break;
                case 14:
                    system += "<li><a class=\"item\"href=\"listWorkArea.aspx\"> - WorkArea</a></li>";
                    break;
            }
        }
        lblMenu.Text = "<div class=\"left\"><ul class=\"title\"><li>Administration Menu</li></ul><ul id=\"menu\"><li>";
        lblMenu.Text += "<a href=\"#\">Welcome</a><ul>";
        lblMenu.Text += welcome;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Security Management</a><ul>";
        lblMenu.Text += segurity;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">System Management</a><ul>";
        lblMenu.Text += system;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Parameters Management</a><ul>";
        lblMenu.Text += parameters;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">People Management</a><ul>";
        lblMenu.Text += people;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Operations Management</a><ul>";
        lblMenu.Text += operations;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Reports</a><ul>";
        lblMenu.Text += reports;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"../index.aspx\">Out</a></li>";
        lblMenu.Text += "</ul></div>";
    }

    //Method to enable calendar
    protected void PopCalendar_SelectionChanged(object sender, EventArgs e)
    {
        this.Validate("RJS.PopCalendars");
    }

    //Method to cancel the view NEW
    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        MOffer.ActiveViewIndex = 0;
    }

    //Method to cancel the view UPDATE
    protected void lnkCancel_Click1(object sender, EventArgs e)
    {
        MOffer.ActiveViewIndex = 0;
    }

    #region Pager

    //Method to query data and create the pager
    private void Bind(String txtSearch)
    {
        DataSet objDS = new DataSet();
        clsOffer objOffer = new clsOffer();

        if (!Page.IsPostBack)
        {
            objDS = objOffer.SeeAllTheRecords(0, 0, txtSearch);
            intRecordCount.Text = objDS.Tables[0].Rows.Count + "";
            objDS = null;
            objDS = new DataSet();
        }

        if (intCurrIndex.Text.Equals("0"))
        {
            hrefPrevious.Visible = false;
        }
        else
        {
            hrefPrevious.Visible = true;
        }

        objDS = objOffer.SeeAllTheRecords(Int32.Parse(intCurrIndex.Text), Int32.Parse(intPageSize.Text), txtSearch);

        DataList1.DataSource = objDS.Tables[0].DefaultView;
        DataList1.DataBind();
        PrintStatus();
    }

    //Method to navigate to the first page of pager
    public void ShowFirst(object sender, EventArgs e)
    {
        intCurrIndex.Text = "0";
        Bind(this.txtSearch.Text);
    }

    //Method to navigate to the previous page in the pager
    public void ShowPrevious(object sender, EventArgs e)
    {
        intCurrIndex.Text = (Int32.Parse(intCurrIndex.Text) - Int32.Parse(intPageSize.Text)) + "";
        if (Int32.Parse(intCurrIndex.Text) < 0)
        {
            intCurrIndex.Text = "0";
        }
        Bind(this.txtSearch.Text);
    }

    //Method to navigate to the next page in the pager
    public void ShowNext(object sender, EventArgs e)
    {
        if ((Int32.Parse(intCurrIndex.Text) + 1) < Int32.Parse(intRecordCount.Text))
        {
            intCurrIndex.Text = (Int32.Parse(intCurrIndex.Text) + Int32.Parse(intPageSize.Text)) + "";
        }
        Bind(this.txtSearch.Text);
    }

    //Method to navigate to the last page of the pager
    public void ShowLast(object sender, EventArgs e)
    {
        int tmpInt = 0;
        tmpInt = Int32.Parse(intRecordCount.Text) % Int32.Parse(intPageSize.Text);
        if (tmpInt > 0)
        {
            intCurrIndex.Text = (Int32.Parse(intRecordCount.Text) - tmpInt) + "";
        }
        else
        {
            intCurrIndex.Text = (Int32.Parse(intRecordCount.Text) - Int32.Parse(intPageSize.Text)) + "";
        }
        Bind(this.txtSearch.Text);
    }

    //Method to generate the data pager navigation
    private void PrintStatus()
    {
        lblTotal.Text = "Total Records:<b> " + intRecordCount.Text + "</b>";
        lblShowing.Text = "Showing Page:<b> ";
        lblShowing.Text += ((Int32.Parse(intCurrIndex.Text) / Int32.Parse(intPageSize.Text)) + 1) + "";
        lblShowing.Text += "</b> of <b>";

        if ((Int32.Parse(intRecordCount.Text) % Int32.Parse(intPageSize.Text)) > 0)
        {
            lblShowing.Text += ((Int32.Parse(intRecordCount.Text) / Int32.Parse(intPageSize.Text)) + 1) + "";
        }
        else
        {
            lblShowing.Text += (Int32.Parse(intRecordCount.Text) / Int32.Parse(intPageSize.Text)) + "";
        }
        lblShowing.Text += "</b>";
    }
    #endregion

    //To search method
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind(this.txtSearch.Text);
    }

    //Method to insert a record in the view NEW
    protected void lnkInsert_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            clsOffer objOffer = new clsOffer();
            objOffer.IPriority = Convert.ToInt32(cbPriority.SelectedValue);
            objOffer.STitle = txtTitle.Text;
            objOffer.SDescription = txtDescription.Text;
            objOffer.SUrlImage = txtUrlImage.Text;
            objOffer.Insert();

            Response.Redirect("listOffers.aspx?message=yes&namenew=" + txtTitle.Text);
        }
        else
        {
            lblMsgValidatorNew.Text = "Some of the required fields are not valid";
        }
    }

    //Method to return the view according to the message
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        if ((Request.Params.Get("nameupdate") != null) || (Request.Params.Get("removal") != null))
            Response.Redirect("listOffers.aspx");
        else
            Response.Redirect("listOffers.aspx?new=yes");
    }

    //Method to update a record in the view UPDATE
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            HiddenField fhidID = (HiddenField)frmvUpdateOffer.FindControl("hidID");
            DropDownList fcbPriority = (DropDownList)frmvUpdateOffer.FindControl("cbpriority");
            TextBox ftxtTitle = (TextBox)frmvUpdateOffer.FindControl("txtTitle");
            TextBox ftxtDescription = (TextBox)frmvUpdateOffer.FindControl("txtDescription");
            TextBox ftxtUrlImage = (TextBox)frmvUpdateOffer.FindControl("txtUrlImage");

            clsOffer objOffer = new clsOffer();
            objOffer.ID = Convert.ToInt32(fhidID.Value);
            objOffer.IPriority = Convert.ToInt32(fcbPriority.SelectedValue);
            objOffer.STitle = ftxtTitle.Text;
            objOffer.SDescription = ftxtDescription.Text;
            objOffer.SUrlImage = ftxtUrlImage.Text;
            objOffer.Update();

            Response.Redirect("listOffers.aspx?message=yes&nameupdate=" + ftxtTitle.Text);
        }
        else
        {
            lblMsgValidatorNew.Text = "Some of the required fields are not valid";
        }
    }

    //Method for the disposal of records
    private void DeleteRecord(String id)
    {
        clsOffer objOffer = new clsOffer();
        objOffer.ID = Convert.ToInt32(id);
        objOffer.Delete();
        Response.Redirect("listOffers.aspx?message=yes&removal=yes");
    }

    //Method to upload files and move the path of this
    protected void lnkUpDoc_Click(object sender, EventArgs e)
    {
        String path = PATHIMAGE + "\\administration\\Images\\Offer\\" + fuImage.FileName;
        fuImage.SaveAs(Server.MapPath(path));
        this.txtUrlImage.Text = "~/administration/Images/Offer/" + fuImage.FileName;
    }

    //Method to upload files and move the path of this
    protected void lnkUpDocUpdate_Click(object sender, EventArgs e)
    {
        TextBox ftxtUrlImage = (TextBox)frmvUpdateOffer.FindControl("txtUrlImage");
        FileUpload ffuImage = (FileUpload)frmvUpdateOffer.FindControl("fuImageUpdate");
        String path = PATHIMAGE + "\\administration\\Images\\Offer\\" + ffuImage.FileName;
        ffuImage.SaveAs(Server.MapPath(path));
        ftxtUrlImage.Text = "~/administration/Images/Offer/" + ffuImage.FileName;
    }
}
